//
//  HWWelfareCenterTopCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/4/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWWelfareCenterTopCell: UITableViewCell {
    
    var bgView:UIView!
    var itemViews:[UIView] = []
    var titleLabels:[UILabel] = []
//    var firstImage:UIImageView!
//    var firstTitle:UILabel!
//    var firstButton:UIButton!
//
//    var secondImage:UIImageView!
//    var secondTitle:UILabel!
//    var secondButton:UIButton!
//
//    var thirdImage:UIImageView!
//    var thirdTitle:UILabel!
//    var thirdButton:UIButton!
    
    var itemClick:((_:HWWelfareCenterDataModel)->())?
    var model:HWWelfareCenterModel? {
        didSet {
//            if (model?.topdata.count ?? 0) >= 1 {
//                firstImage.sd_setImage(with: URL(string: model?.topdata[0].icon ?? ""), completed: nil)
//                firstTitle.text = model?.topdata[0].name
//            }
//            if (model?.topdata.count ?? 0) >= 2 {
//                secondImage.sd_setImage(with: URL(string: model?.topdata[1].icon ?? ""), completed: nil)
//                secondTitle.text = model?.topdata[1].name
//            }
//            if (model?.topdata.count ?? 0) >= 3 {
//                thirdImage.sd_setImage(with: URL(string: model?.topdata[2].icon ?? ""), completed: nil)
//                thirdTitle.text = model?.topdata[2].name
//            }
            for iv in itemViews {
                iv.removeFromSuperview()
            }
            itemViews.removeAll()
            
            var tempView:UIView!
            for i in 0..<(model?.topdata.count ?? 0) {
                let itemView = UIView()
                bgView.addSubview(itemView)
                let imageView = UIImageView()
                itemView.addSubview(imageView)
                imageView.sd_setImage(with: URL(string: model?.topdata[i].icon ?? ""), completed: nil)
                
                let titleLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
                itemView.addSubview(titleLabel)
                titleLabel.text = model?.topdata[i].name
                
                let button = UIButton(type: .custom)
                button.addTarget(self, action: #selector(itemButtonClick(_:)), for: .touchUpInside)
                button.tag = i
                itemView.addSubview(button)
                
                if tempView != nil {
                    if (model?.topdata.count ?? 0) > 3 {
                        if i % 3 == 0 {
                            itemView.snp.makeConstraints { make in
                                make.left.equalToSuperview()
                                make.top.equalTo(tempView.snp.bottom)
                                make.width.equalTo(screenWidth/3)
                                make.height.equalTo(90*widthScale)
                            }
                        }else {
                            itemView.snp.makeConstraints { make in
                                make.left.equalTo(tempView.snp.right)
                                make.top.equalTo(tempView)
                                make.width.equalTo(screenWidth/3)
                                make.height.equalTo(90*widthScale)
                            }
                        }
                    }else {
                        if i % (model?.topdata.count ?? 0) == 0 {
                            itemView.snp.makeConstraints { make in
                                make.left.equalToSuperview()
                                make.top.equalTo(tempView.snp.bottom)
                                make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                                make.height.equalTo(90*widthScale)
                            }
                        }else {
                            itemView.snp.makeConstraints { make in
                                make.left.equalTo(tempView.snp.right)
                                make.top.equalTo(tempView)
                                make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                                make.height.equalTo(90*widthScale)
                            }
                        }
                    }
                    
                }else {
                    itemView.snp.makeConstraints { make in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.height.equalTo(90*widthScale)
                        if (model?.topdata.count ?? 0) > 3 {
                            make.width.equalTo(screenWidth/3)
                        }else {
                            make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                        }
                    }
                }
                
                imageView.snp.makeConstraints { make in
                    make.top.equalTo(8*widthScale)
                    make.centerX.equalToSuperview()
                    make.width.height.equalTo(52*widthScale)
                }
                
                titleLabel.snp.makeConstraints { make in
                    make.centerX.equalTo(imageView)
                    make.top.equalTo(imageView.snp.bottom)
                }
                
                button.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                
                tempView = itemView
                itemViews.append(itemView)
                titleLabels.append(titleLabel)
            }
            if itemViews.count > 0 {
                itemViews.last?.snp.makeConstraints({ make in
                    make.bottom.equalToSuperview()
                })
                
//                titleLabels.last?.snp.makeConstraints({ make in
//                    make.bottom.equalTo(-10*widthScale)
//                })
            }
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWWelfareCenterTopCell) {
        let reUse:String = "HWWelfareCenterTopCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWWelfareCenterTopCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWWelfareCenterTopCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        
        bgView = UIView()
        contentView.addSubview(bgView)
        
//        firstImage = UIImageView()
//        bgView.addSubview(firstImage)
//
//        firstTitle = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
//        bgView.addSubview(firstTitle)
//
//        firstButton = UIButton(type: .custom)
//        firstButton.addTarget(self, action: #selector(firstButtonClick(_:)), for: .touchUpInside)
//        bgView.addSubview(firstButton)
//
//        secondImage = UIImageView()
//        bgView.addSubview(secondImage)
//
//        secondTitle = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
//        bgView.addSubview(secondTitle)
//
//        secondButton = UIButton(type: .custom)
//        secondButton.addTarget(self, action: #selector(secondButtonClick(_:)), for: .touchUpInside)
//        bgView.addSubview(secondButton)
//
//        thirdImage = UIImageView()
//        bgView.addSubview(thirdImage)
//
//        thirdTitle = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
//        bgView.addSubview(thirdTitle)
//
//        thirdButton = UIButton(type: .custom)
//        thirdButton.addTarget(self, action: #selector(thirdButtonClick(_:)), for: .touchUpInside)
//        bgView.addSubview(thirdButton)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
//            make.height.equalTo(90*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
//        firstImage.snp.makeConstraints { make in
//            make.left.equalTo(22*widthScale)
//            make.top.equalTo(8*widthScale)
//            make.width.height.equalTo(52*widthScale)
//        }
//
//        firstTitle.snp.makeConstraints { make in
//            make.centerX.equalTo(firstImage)
//            make.top.equalTo(firstImage.snp.bottom)
//        }
//
//        firstButton.snp.makeConstraints { make in
//            make.left.top.right.equalTo(firstImage)
//            make.bottom.equalTo(firstTitle)
//        }
//
//        secondImage.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(8*widthScale)
//            make.width.height.equalTo(52*widthScale)
//        }
//
//        secondTitle.snp.makeConstraints { make in
//            make.centerX.equalTo(secondImage)
//            make.top.equalTo(secondImage.snp.bottom)
//        }
//
//        secondButton.snp.makeConstraints { make in
//            make.left.top.right.equalTo(secondImage)
//            make.bottom.equalTo(secondTitle)
//        }
//
//        thirdImage.snp.makeConstraints { make in
//            make.right.equalTo(-22*widthScale)
//            make.top.equalTo(8*widthScale)
//            make.width.height.equalTo(52*widthScale)
//        }
//
//        thirdTitle.snp.makeConstraints { make in
//            make.centerX.equalTo(thirdImage)
//            make.top.equalTo(thirdImage.snp.bottom)
//        }
//
//        thirdButton.snp.makeConstraints { make in
//            make.left.top.right.equalTo(thirdImage)
//            make.bottom.equalTo(thirdTitle)
//        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        bgView.layoutIfNeeded()
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithHexString(hexString: "#494A56").cgColor,UIColor.colorWithHexString(hexString: "#20202A").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    @objc func firstButtonClick(_ button:UIButton) -> Void {
//        self.itemClick?(model!.topdata[0])
//    }
//
//    @objc func secondButtonClick(_ button:UIButton) -> Void {
//        self.itemClick?(model!.topdata[1])
//    }
//
//    @objc func thirdButtonClick(_ button:UIButton) -> Void {
//        self.itemClick?(model!.topdata[2])
//    }
    
    @objc func itemButtonClick(_ button:UIButton) -> Void {
        self.itemClick?(model!.topdata[button.tag])
    }

}
